package com.commons.tools.multiconn;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>根据参数不同调用不同的实现类，applicationContext.getBean()</p>
 */
@RestController
@RequestMapping("/multiconn")
public class MultiConnController implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @RequestMapping("/type/{type}")
    public void multiconnTest(@PathVariable String type) {
        IService iService;
        if (type.equals("cmp")) {
            iService = applicationContext.getBean(AServiceImpl.class);
        } else {
            iService = applicationContext.getBean(BServiceImpl.class);
        }
        iService.query();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
